package cn.itsource.org.controller;
import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.TDepartment;
import cn.itsource.org.query.TDepartmentQuery;
import cn.itsource.org.service.TDepartmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@Validated
@RequestMapping("/department")
public class TDepartmentController {
    @Autowired
    private TDepartmentService tDepartmentService;

    /**
     * 增加或修改
     *
     * @return
     */
    @PutMapping("/save")
    public AjaxResult save(@RequestBody @Validated TDepartment tDepartment) {
        tDepartmentService.save(tDepartment);
        return new AjaxResult().setSuccess(true).setMessage("保存成功");
    }

    /**
     * 根据id删除
     *
     * @param id
     * @return
     */
    @DeleteMapping("/delete/{id}")
    public AjaxResult delete(@PathVariable("id") Long id) {
        tDepartmentService.delete(id);
        return new AjaxResult().setSuccess(true).setMessage("删除成功");
    }
    /**
     * 根据id查询
     *
     * @param id
     * @return
     */
    @GetMapping("/selectById/{id}")
    public AjaxResult selectById(@PathVariable("id") Long id) {
        return new AjaxResult().me().setResultObj(tDepartmentService.selectById(id));
    }

    /**
     * 查询所有
     */
    @GetMapping("/selectAll")
    public AjaxResult selectAll() {
        return new AjaxResult().me().setResultObj(tDepartmentService.selectAll());
    }

    /**
     * 批量删除
     *
     * @return
     */
    @PatchMapping("/patchDelete")
    public AjaxResult patchDelete(@RequestBody List<Long> ids) {
        tDepartmentService.patchDelete(ids);
        return new AjaxResult().setSuccess(true).setMessage("批量删除成功");
    }

    /**
     * 分页查询
     */
    @PostMapping("/selectByQueryPage")
    public AjaxResult selectByPage(@RequestBody TDepartmentQuery tDepartmentQuery) {
        return new AjaxResult().me().setResultObj(tDepartmentService.selectByQueryPage(tDepartmentQuery));
    }
    /**
     * 无限级树
     */

    @GetMapping("/deptTree")
    public AjaxResult DeptTree() {
        return new AjaxResult().me().setResultObj(tDepartmentService.deptTree());
    }
}
